package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/updateEmployee")
public class UpdateEmployeeServlet extends HttpServlet {

    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求编码为UTF-8
        request.setCharacterEncoding("UTF-8");
        
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();
        
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.print(new Gson().toJson(result));
            return;
        }
        
        User currentUser = (User) session.getAttribute("user");
        
        // 检查是否是公司负责人
        if (currentUser.getRole() != 1) {
            result.put("success", false);
            result.put("message", "只有公司负责人可以更新员工信息");
            out.print(new Gson().toJson(result));
            return;
        }
        
        String companyId = currentUser.getCompanyId();
        if (companyId == null || companyId.isEmpty()) {
            result.put("success", false);
            result.put("message", "您尚未加入公司");
            out.print(new Gson().toJson(result));
            return;
        }
        
        // 获取请求参数
        String employeeId = request.getParameter("id");
        String position = request.getParameter("position");
        
        // 对职位进行URL解码
        if (position != null) {
            position = URLDecoder.decode(position, "UTF-8");
        }
        
        String salaryStr = request.getParameter("salary");
        
        // 参数验证
        if (employeeId == null || employeeId.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "员工ID不能为空");
            out.print(new Gson().toJson(result));
            return;
        }
        
        if (position == null || position.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "职位不能为空");
            out.print(new Gson().toJson(result));
            return;
        }
        
        BigDecimal salary;
        try {
            salary = new BigDecimal(salaryStr);
            if (salary.compareTo(BigDecimal.ZERO) < 0) {
                result.put("success", false);
                result.put("message", "薪资不能为负数");
                out.print(new Gson().toJson(result));
                return;
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "薪资格式不正确");
            out.print(new Gson().toJson(result));
            return;
        }
        
        // 获取员工详情，检查是否是负责人
        CompanyEmployee employee = companyEmployeeService.getEmployeeDetail(employeeId);
        if (employee == null) {
            result.put("success", false);
            result.put("message", "找不到该员工");
            out.print(new Gson().toJson(result));
            return;
        }
        
        // 检查是否是修改负责人信息
        if ("负责人".equals(employee.getPosition())) {
            result.put("success", false);
            result.put("message", "不能修改负责人的职位和薪资");
            out.print(new Gson().toJson(result));
            return;
        }
        
        // 更新员工信息
        boolean success = companyEmployeeService.updateEmployeeInfo(employeeId, position, salary);
        
        if (success) {
            result.put("success", true);
            result.put("message", "员工信息更新成功");
        } else {
            result.put("success", false);
            result.put("message", "员工信息更新失败，请稍后重试");
        }
        
        out.print(new Gson().toJson(result));
    }
} 